package com.atguigu.gmallre.mock.service.impl;

import com.atguigu.gmallre.mock.bean.*;
import com.atguigu.gmallre.mock.mapper.*;
import com.atguigu.gmallre.mock.service.*;
import com.atguigu.gmallre.mock.service.adv.impl.AdvServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 * 购物车表 用户登录系统时更新冗余 服务实现类
 * </p>
 *
 * @author zc
 * @since 2020-02-24
 */
@Service
@Slf4j
public class CartInfoServiceImpl extends AdvServiceImpl<CartInfoMapper, CartInfo> implements CartInfoService {
    @Autowired
    SkuInfoService skuInfoService;

    @Autowired
    UserInfoService userInfoService;


    Map<Long,List<CartInfo>>  cartInfoByUserMap=new HashMap<>();

    @PostConstruct
    protected void loadCache(){
        List<CartInfo> list = list();
        for (CartInfo cartInfo : list) {
            List<CartInfo> cartInfoList = cartInfoByUserMap.get(cartInfo.getUserId());
            if(cartInfoList==null){
                cartInfoList = new ArrayList<>();
            }
            cartInfoList.add(cartInfo);
            cartInfoByUserMap.put(cartInfo.getUserId(),cartInfoList);
        }

    }

    public List<CartInfo> getCartInfoListByUserId(Long userId){
       return cartInfoByUserMap.get(userId);
    }












}
